package com.jic.market.mapper;

import com.jic.market.entity.DiscountProductSingleDetail;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 优惠商品明细表(包含单品类活动)
 * @author : 
 * @email : 
 * @since : 2020-04-30 11:43:27
 * @version : v1.0.0
 */
@Mapper
public interface  DiscountProductSingleDetailMapper {

    long insert(DiscountProductSingleDetail discountProductSingleDetail);

    long delete(Long id);

    long update(DiscountProductSingleDetail discountProductSingleDetail);

    DiscountProductSingleDetail queryByPrimaryKey(Long id);

    List<DiscountProductSingleDetail> query(DiscountProductSingleDetail discountProductSingleDetail);

    List<DiscountProductSingleDetail> queryPage(@Param("startIndex") int startIndex, @Param("limit") int limit, @Param("discountProductSingleDetail") DiscountProductSingleDetail discountProductSingleDetail);

    long queryPageCount(DiscountProductSingleDetail discountProductSingleDetail);


    @Select("select sum(s.single_valid_num) from discount_product_single_detail s LEFT JOIN discount_order_detail o ON s.order_no = o.order_no " +
            "where s.promotion_id = #{promotionId} and s.third_sku_id = #{thirdSkuId} and o.third_user_id = #{thirdUserId} and s.delete_flag=0 and o.delete_flag=0 and o.discount_order_status != 3")
    Integer countSingleValidNumByPromotionIdAndThirdSkuIdAndThirdUserId(@Param("promotionId")String promotionId, @Param("thirdSkuId")String thirdSkuId, @Param("thirdUserId")String thirdUserId);

    @Select("select sum(single_valid_num) from discount_product_single_detail where promotion_id = #{promotionId} and third_sku_id = #{thirdSkuId} and delete_flag=0 and single_discount_detail_status != 3")
    Integer countSingleValidNumNumByPromotionIdAndThirdSkuId(@Param("promotionId")String promotionId, @Param("thirdSkuId")String thirdSkuId);

    @Select("select sum(s.condition_valid_num) from discount_product_single_detail s LEFT JOIN discount_condition_promotion_detail d ON s.order_no = d.order_no LEFT JOIN discount_order_detail o ON s.order_no = o.order_no " +
            "where d.promotion_id = #{promotionId} and s.third_sku_id = #{thirdSkuId} and o.third_user_id = #{thirdUserId} and s.delete_flag=0 and o.delete_flag=0 and o.discount_order_status != 3")
    Integer countConditionValidNumByPromotionIdAndThirdSkuIdAndThirdUserId(@Param("promotionId")String promotionId, @Param("thirdSkuId")String thirdSkuId, @Param("thirdUserId")String thirdUserId);

    @Select("select sum(s.condition_valid_num) from discount_product_single_detail s LEFT JOIN discount_condition_promotion_detail d ON s.order_no = d.order_no LEFT JOIN discount_order_detail o ON s.order_no = o.order_no " +
            "where d.promotion_id = #{promotionId} and s.third_sku_id = #{thirdSkuId} and s.delete_flag=0 and o.delete_flag=0 and o.discount_order_status != 3")
    Integer countConditionValidNumNumByPromotionIdAndThirdSkuId(@Param("promotionId")String promotionId, @Param("thirdSkuId")String thirdSkuId);


//    @Select("select sum(s.condition_valid_num) from discount_product_single_detail s LEFT JOIN discount_condition_promotion_detail d ON s.order_no = d.order_no LEFT JOIN discount_order_detail o ON s.order_no = o.order_no " +
//            "LEFT JOIN discount_product_single_share_detail sh ON sh.promotion_id = d.promotion_id "+
//            "where d.promotion_id = #{promotionId} and s.third_sku_id = #{thirdSkuId} and s.delete_flag=0 and o.delete_flag=0 and sh.delete_flag=0 and sh.condition_discount_detail_status != 3")
//    Integer countShareConditionValidNumNumByPromotionIdAndThirdSkuId(@Param("promotionId")String promotionId, @Param("thirdSkuId")String thirdSkuId);
//
//    @Select("select sum(s.condition_valid_num) from discount_product_single_detail s LEFT JOIN discount_condition_promotion_detail d ON s.order_no = d.order_no LEFT JOIN discount_order_detail o ON s.order_no = o.order_no " +
//            "LEFT JOIN discount_product_single_share_detail sh ON sh.promotion_id = d.promotion_id "+
//            "where d.promotion_id = #{promotionId} and s.third_sku_id = #{thirdSkuId} and o.third_user_id = #{thirdUserId} and s.delete_flag=0 and o.delete_flag=0 and sh.delete_flag=0 and sh.condition_discount_detail_status != 3")
//    Integer countShareConditionValidNumByPromotionIdAndThirdSkuIdAndThirdUserId(@Param("promotionId")String promotionId, @Param("thirdSkuId")String thirdSkuId, @Param("thirdUserId")String thirdUserId);

}